package com.shujia.dao.impl;

import com.shujia.bean.Dept;
import com.shujia.dao.CacheDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.Types;
import java.util.Map;

@Component
public class CacheDaoImpl implements CacheDao {

    // 如果需要操作MySQL，那么需要构建JDBC对象,需要添加Spring JDBC依赖
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public String getDeptByDeptNo(int deptNo) {
        System.out.println("Dao层中的getDeptByDeptNo方法被调用...");
        Map<String, Object> deptMap = jdbcTemplate.queryForMap("SELECT * FROM dept WHERE deptno = ?", deptNo);
        String dname = (String) deptMap.get("dname");
        return dname;
    }

    @Override
    public void updateDept(Dept dept) {
        // 更新数据时，只支持两个参数值
        jdbcTemplate.update(
                "UPDATE dept SET dname=? WHERE deptno=? ",
                new Object[]{
                        dept.getdName(),
                        dept.getDeptNo()
                },
                new int[]{
                        Types.VARCHAR,
                        Types.INTEGER
                }
        );

    }


}
